package com.nic.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.nic.view.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/22.
 */

public abstract class LoadMoreAdapter<T> extends BaseAdapter {

    protected List<T> list = new ArrayList<>();
    protected Context context;
    protected boolean isLoadFinish;//是否加载结束
    private int pageSize;

    public LoadMoreAdapter(Context context, int pageSize) {
        this.context = context;
        this.pageSize = pageSize;
    }

    public void setList(List<T> list, boolean isLoadMore) {
        if (list == null || list.isEmpty()) {
            if (isLoadMore) {
                isLoadFinish = true;
                notifyDataSetChanged();
            }
            return;
        }
        if (isLoadMore) {
            this.list.addAll(list);
        } else {
            this.list = list;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position >= list.size()) {
            return getCount();
        }
        return super.getItemViewType(position);
    }

    @Override
    public Object getItem(int i) {
        return i < list.size() ? list.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        int viewType = getItemViewType(position);
        if (viewType == getCount()) {
            //底部布局填充
            AdapterView.LayoutParams params = new AdapterView.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_load_more, null);
            view.setLayoutParams(params);
            BottomViewHolder bottomViewHolder = new BottomViewHolder(view);
            if (list.isEmpty()) {
                bottomViewHolder.tvLoadMore.setVisibility(View.GONE);
                bottomViewHolder.loadingBar.setVisibility(View.GONE);
//                bottomViewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
            } else {
//                bottomViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_200));
                if (getCount() < pageSize || isLoadFinish) {
                    bottomViewHolder.tvLoadMore.setVisibility(View.VISIBLE);
                    bottomViewHolder.loadingBar.setVisibility(View.GONE);
                } else {
                    bottomViewHolder.loadingBar.setVisibility(View.VISIBLE);
                    bottomViewHolder.tvLoadMore.setVisibility(View.GONE);
                }
            }
        } else {
            //内容填充
            ViewHolder holder;
            if (view.getTag() == null || view.getTag() instanceof LoadMoreAdapter.BottomViewHolder) {
                holder = createViewHolder(viewGroup, viewType);
                view = holder.itemView;
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            bindViewHolder(viewGroup, position);
        }
        return view;
    }

    /**
     * 创建
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    public abstract ViewHolder createViewHolder(ViewGroup viewGroup, int viewType);

    /**
     * 绑定
     *
     * @param viewGroup
     * @param position
     */
    public abstract void bindViewHolder(ViewGroup viewGroup, int position);

    public static class ViewHolder {
        View itemView;

        public ViewHolder(View view) {
            this.itemView = view;
        }
    }

    class BottomViewHolder extends ViewHolder {
        ContentLoadingProgressBar loadingBar;
        TextView tvLoadMore;

        public BottomViewHolder(@NonNull View itemView) {
            super(itemView);
            loadingBar = itemView.findViewById(R.id.loadingBar);
            tvLoadMore = itemView.findViewById(R.id.tv_load_more);
        }
    }
}
